package com.xszx.cms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xszx.cms.entity.Banner;
import com.xszx.cms.mapper.BannerMapper;
import com.xszx.cms.service.BannerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;


import java.util.List;

/**
 * <p>
 * 首页banner表 服务实现类
 * </p>
 *
 * @author 白熊
 * @since 2024-07-22
 */
@Service
public class BannerServiceImpl extends ServiceImpl<BannerMapper, Banner> implements BannerService {

    @Override//注意注解
    @Cacheable(value = "banner", key = "'bannerList'")
    public List<Banner> selectAllBanner() {
        //根据id进行降序排列，显示排列之后前两条记录
        QueryWrapper<Banner> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id");
        //last方法，拼接sql语句
        wrapper.last(" limit 4 ");
        List<Banner> list = baseMapper.selectList(null);
        return list;
    }


    @Override//分页查询
    public void pageBanner(Page<Banner> pageParam,QueryWrapper queryWrapper) {
        baseMapper.selectPage(pageParam, null);
    }

    @Override
    public Banner getBannerById(String id) {
        return baseMapper.selectById(id);
    }

    @CacheEvict(value = "banner", allEntries = true)
    @Override
    public void saveBanner(Banner banner) {
        baseMapper.insert(banner);
    }

    @CacheEvict(value = "banner", allEntries = true)
    @Override
    public void updateBannerById(Banner banner) {
        baseMapper.updateById(banner);
    }

    @CacheEvict(value = "banner", allEntries = true)
    @Override
    public void removeBannerById(String id) {
        baseMapper.deleteById(id);
    }
}
